<template>
  <div>
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onUpdateName"
    />
    <van-field
      v-model.trim="message"
      rows="1"
      autosize
      label="昵称"
      type="textarea"
      maxlength="7"
      placeholder="请输入昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { updateUserFile } from '@/api/user'
export default {
  data () {
    return {
      message: this.value
    }
  },
  props: {
    value: String
  },
  methods: {
    async onUpdateName () {
      this.$toast.loading({
        message: '更新中...',
        forbidClick: true,
        duration: 0
      })
      try {
        if (!this.message) {
          return this.$toast('昵称不能为空')
        }
        await updateUserFile({
          name: this.message
        })
        this.$toast('更新成功')
        this.$emit('input', this.message)
      } catch (err) {
        this.$toast('更新失败，请重试')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-cell {
  background-color: springgreen;
}
/deep/.van-nav-bar__title {
  color: #999!important;
}
</style>
